package com.nxy.cpfsfe.busi.btmgtsk.endd;

import com.adtec.starring.datapool.EPOper;
import com.adtec.starring.exception.BaseException;
import com.adtec.starring.struct.dta.DtaInfo;
import com.adtec.starring.util.SpringUtil;
import com.nxy.cpfsfe.cache.DbToParmToCache;
import com.nxy.cpfsfe.enumtype.*;
import com.nxy.cpfsfe.mybatis.mapper.TbAMapper;
import com.nxy.cpfsfe.mybatis.mapper.TbBizChkaSumMapper;
import com.nxy.cpfsfe.mybatis.mapper.ToBizSysMapper;
import com.nxy.cpfsfe.mybatis.pojo.TbA;
import com.nxy.cpfsfe.mybatis.pojo.TbBizChkaSum;
import com.nxy.cpfsfe.mybatis.pojo.TeBizSett;
import com.nxy.cpfsfe.mybatis.pojo.ToParm;
import com.nxy.cpfsfe.util.*;
import com.nxy.cpfsfe.util.sftp.SFTPTransfer;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;

import java.io.File;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class NCSEndDChkASrv extends AbsEndDChkASrv {
	
	private static final Logger LOGGER = LoggerFactory.getLogger(NCSEndDChkASrv.class);

	private final static Logger monitorLOGGER = LoggerFactory.getLogger("monitor");
	
	public NCSEndDChkASrv(){}
	
	public NCSEndDChkASrv(String bizSysId,String pCId,Date chkAD,String starringModelNm){
		this.bizSysId = bizSysId;
		this.pCId = pCId;
		this.chkAD = chkAD;
		this.sqlSession = (SqlSession) SpringUtil.getBean("mybatisSqlSession");
		this.teBizSett = new TeBizSett();
		this.teBizSett.setBizSysId(bizSysId);
		this.teBizSett.setpCId(pCId);
		this.teBizSett.setChkAD(chkAD);
		this.starringModelNm = starringModelNm;
	}

	/**
	 *
	 * Description:核对总笔数和总金额是否与NCS一致
	 *
	 * @author jjw
	 * @data 2019年11月07日
	 */
	@Override
	protected void chkNumAndAmt(){

		JdbcTemplate jdbcTemplate = (JdbcTemplate) SpringUtil.getBean("jdbcTemplate");
		ToBizSysMapper toBizSysMapper = sqlSession.getMapper(ToBizSysMapper.class);

		List<String> bizSysIds = toBizSysMapper.queryAllEnbBizSys();

		//汇兑往账
		String rmSndSql = "select count(id) drTotDN,sum(xAmt) drTotAmt " +
				"from tb_a " +
				"where chlXCd = '" + MsgCdOrTpEnum.MSGCD_120.getCode() + "' and xDirn = '12' and pCId = ? and cpfsD = ? and acqOrgId = acqOrgId ";
		//正常交易
		String rmSndBizSql = rmSndSql + "and chkASta = '1' and sndSysId in (";
		//异常往账
		String rmSndExceSql = rmSndSql + "and chkASta = '3'";
		//资金归集
		String rmSndGJSql = rmSndSql + "and chkASta = '1' and reserved1 = '" + Constant.ZJGJ_FLG + "'";


		//汇兑来账
		String rmRcvSql = "select count(id) loanTotDN,sum(xAmt) loanTotAmt " +
				"from tb_a " +
				"where chlXCd = '" + MsgCdOrTpEnum.MSGCD_120.getCode() + "' and xDirn = '21' and pCId = ? and cpfsD = ? and acqOrgId = acqOrgId ";
		//正常交易
		String rmRcvBizSql = rmRcvSql + "and chkASta = '1' and rcvSysId in (";
		//异常来账
		String rmRcvExceSql = rmRcvSql + "and chkASta = '3'";
		//资金归集
		String rmRcvGJSql = rmRcvSql + "and chkASta = '1' and reserved1 = '" + Constant.ZJGJ_FLG + "'";


		//借记往账
		String debitSndSql = "select count(id) loanTotDN,sum(xAmt) loanTotAmt " +
				"from tb_a " +
				"where chlXCd = '" + MsgCdOrTpEnum.MSGCD_142.getCode() + "' and xDirn = '12' and pCId = ? and cpfsD = ? and acqOrgId = acqOrgId ";
		//正常交易
		String debitSndBizSql = debitSndSql + "and chkASta = '1' and sndSysId in (";
		//异常往账
		String debitSndExceSql = debitSndSql + "and chkASta = '3'";


		//贷记往账
		String creditSndSql = "select count(id) drTotDN,sum(xAmt) drTotAmt " +
				"from tb_a " +
				"where chlXCd = '" + MsgCdOrTpEnum.MSGCD_140.getCode() + "' and xDirn = '12' and pCId = ? and cpfsD = ? and acqOrgId = acqOrgId ";
		//正常交易
		String creditSndBizSql = creditSndSql + "and chkASta = '1' and sndSysId in (";
		//异常往账
		String creditSndExceSql = creditSndSql + "and chkASta = '3'";


		//ncs汇兑往账
		String ncsRmSndSql = "select ifnull(sum(drTotDN),0) drTotDN,ifnull(sum(drTotAmt),0.00) drTotAmt " +
							 "from td_chka_sum " +
							 "where chkAD = ? and msgNo = '" + MsgCdOrTpEnum.MSGCD_120.getCode() + "' and contFlg = '0'";

		//ncs汇兑来账
		String ncsRmRcvSql = "select ifnull(sum(loanTotDN),0) loanTotDN,ifnull(sum(loanTotAmt),0.00) loanTotAmt " +
							 "from td_chka_sum " +
							 "where chkAD = ? and msgNo = '" + MsgCdOrTpEnum.MSGCD_120.getCode() + "' and contFlg = '1'";

		//ncs借记往账
		String ncsDebitSndSql = "select ifnull(sum(loanTotDN),0) loanTotDN,ifnull(sum(loanTotAmt),0.00) loanTotAmt " +
								"from td_chka_sum " +
								"where chkAD = ? and msgNo = '" + MsgCdOrTpEnum.MSGCD_142.getCode() + "' and contFlg = '0'";

		//ncs贷记往账
		String ncsCreditSndSql = "select ifnull(sum(drTotDN),0) drTotDN,ifnull(sum(drTotAmt),0.00) drTotAmt " +
							 	 "from td_chka_sum " +
								 "where chkAD = ? and msgNo = '" + MsgCdOrTpEnum.MSGCD_140.getCode() + "' and contFlg = '0'";

		if(bizSysIds != null && bizSysIds.size() > 0){
			for(String bizSysId : bizSysIds){
				rmSndBizSql += "'" + bizSysId + "',";
				rmRcvBizSql += "'" + bizSysId + "',";
				debitSndBizSql += "'" + bizSysId + "',";
				creditSndBizSql += "'" + bizSysId + "',";
			}

			rmSndBizSql = rmSndBizSql.substring(0,rmSndBizSql.length() - 1) + ")";
			rmRcvBizSql = rmRcvBizSql.substring(0,rmRcvBizSql.length() - 1) + ")";
			debitSndBizSql = debitSndBizSql.substring(0,debitSndBizSql.length() - 1) + ")";
			creditSndBizSql = creditSndBizSql.substring(0,creditSndBizSql.length() - 1) + ")";
		}

		List<Map<String,Object>> rmSndBizQR = jdbcTemplate.queryForList(rmSndBizSql,pCId,chkAD);
		List<Map<String,Object>> rmSndExceQR = jdbcTemplate.queryForList(rmSndExceSql,pCId,chkAD);
		List<Map<String,Object>> rmSndGJQR = jdbcTemplate.queryForList(rmSndGJSql,pCId,chkAD);

		List<Map<String,Object>> rmRcvBizQR = jdbcTemplate.queryForList(rmRcvBizSql,pCId,chkAD);
		List<Map<String,Object>> rmRcvExceQR = jdbcTemplate.queryForList(rmRcvExceSql,pCId,chkAD);
		List<Map<String,Object>> rmRcvGJQR = jdbcTemplate.queryForList(rmRcvGJSql,pCId,chkAD);

		List<Map<String,Object>> debitSndBizQR = jdbcTemplate.queryForList(debitSndBizSql,pCId,chkAD);
		List<Map<String,Object>> debitSndExceQR = jdbcTemplate.queryForList(debitSndExceSql,pCId,chkAD);

		List<Map<String,Object>> creditSndBizQR = jdbcTemplate.queryForList(creditSndBizSql,pCId,chkAD);
		List<Map<String,Object>> creditSndExceQR = jdbcTemplate.queryForList(creditSndExceSql,pCId,chkAD);

		List<Map<String,Object>> ncsRmSndQR = jdbcTemplate.queryForList(ncsRmSndSql,chkAD);
		List<Map<String,Object>> ncsRmRcvQR = jdbcTemplate.queryForList(ncsRmRcvSql,chkAD);
		List<Map<String,Object>> ncsDebitSndQR = jdbcTemplate.queryForList(ncsDebitSndSql,chkAD);
		List<Map<String,Object>> ncsCreditSndQR = jdbcTemplate.queryForList(ncsCreditSndSql,chkAD);


		Long rmSndBizDN = (Long)rmSndBizQR.get(0).get("drTotDN");
		Long rmSndExceDN = (Long)rmSndExceQR.get(0).get("drTotDN");
		Long rmSndGJDN = (Long)rmSndGJQR.get(0).get("drTotDN");

		Long rmRcvBizDN = (Long)rmRcvBizQR.get(0).get("loanTotDN");
		Long rmRcvExceDN = (Long)rmRcvExceQR.get(0).get("loanTotDN");
		Long rmRcvGJDN = (Long)rmRcvGJQR.get(0).get("loanTotDN");

		Long debitSndBizDN = (Long)debitSndBizQR.get(0).get("loanTotDN");
		Long debitSndExceDN = (Long)debitSndExceQR.get(0).get("loanTotDN");

		Long creditSndBizDN = (Long)creditSndBizQR.get(0).get("drTotDN");
		Long creditSndExceDN = (Long)creditSndExceQR.get(0).get("drTotDN");

		Long ncsRmSndDN = ((BigDecimal)ncsRmSndQR.get(0).get("drTotDN")).longValue();
		Long ncsRmRcvDN = ((BigDecimal)ncsRmRcvQR.get(0).get("loanTotDN")).longValue();
		Long ncsDebitSndDN = ((BigDecimal)ncsDebitSndQR.get(0).get("loanTotDN")).longValue();
		Long ncsCreditSndDN = ((BigDecimal)ncsCreditSndQR.get(0).get("drTotDN")).longValue();

		BigDecimal rmRcvBizAmt = ((BigDecimal)rmRcvBizQR.get(0).get("loanTotAmt"));
		BigDecimal rmRcvExceAmt = ((BigDecimal)rmRcvExceQR.get(0).get("loanTotAmt"));
		BigDecimal rmRcvGJAmt = ((BigDecimal)rmRcvGJQR.get(0).get("loanTotAmt"));

		BigDecimal debitSndBizAmt = ((BigDecimal)debitSndBizQR.get(0).get("loanTotAmt"));
		BigDecimal debitSndExceAmt = ((BigDecimal)debitSndExceQR.get(0).get("loanTotAmt"));

		BigDecimal rmSndBizAmt = ((BigDecimal)rmSndBizQR.get(0).get("drTotAmt"));
		BigDecimal rmSndExceAmt = ((BigDecimal)rmSndExceQR.get(0).get("drTotAmt"));
		BigDecimal rmSndGJAmt = ((BigDecimal)rmSndGJQR.get(0).get("drTotAmt"));

		BigDecimal creditSndBizAmt = ((BigDecimal)creditSndBizQR.get(0).get("drTotAmt"));
		BigDecimal creditSndExceAmt = ((BigDecimal)creditSndExceQR.get(0).get("drTotAmt"));

		BigDecimal ncsRmRcvAmt = ((BigDecimal)ncsRmRcvQR.get(0).get("loanTotAmt"));
		BigDecimal ncsDebitSndAmt = ((BigDecimal)ncsDebitSndQR.get(0).get("loanTotAmt"));
		BigDecimal ncsRmSndAmt = ((BigDecimal)ncsRmSndQR.get(0).get("drTotAmt"));
		BigDecimal ncsCreditSndAmt = ((BigDecimal)ncsCreditSndQR.get(0).get("drTotAmt"));

		Long cpfsNumTot = rmSndBizDN + rmSndExceDN + rmSndGJDN +
						  rmRcvBizDN + rmRcvExceDN + rmRcvGJDN +
						  debitSndBizDN + debitSndExceDN + creditSndBizDN + creditSndExceDN;
		Long ncsNumTot = ncsRmSndDN + ncsRmRcvDN + ncsDebitSndDN + ncsCreditSndDN;

		BigDecimal cpfsAmtTot = rmRcvBizAmt.add(rmRcvExceAmt).add(rmRcvGJAmt).add(debitSndBizAmt).add(debitSndExceAmt)
								.subtract((rmSndBizAmt.add(rmSndExceAmt).add(rmSndGJAmt).add(creditSndBizAmt).add(creditSndExceAmt)));
		BigDecimal ncsAmtTot = ncsRmRcvAmt.add(ncsDebitSndAmt).subtract((ncsRmSndAmt.add(ncsCreditSndAmt)));


		int itemLength = 50;

		String cpfsHead1 = "前置笔数";
		String cpfsHead2 = "前置金额";
		String cpfsRmSndBizDNMsg = "正常-汇兑往账:" + rmSndBizDN;
		String cpfsRmSndExceDNMsg = "异常-汇兑往账:" + rmSndExceDN;
		String cpfsRmSndGJDNMsg = "归集-汇兑往账:" + rmSndGJDN;
		String cpfsRmSndBizAmtMsg = "正常-汇兑往账:" + rmSndBizAmt;
		String cpfsRmSndExceAmtMsg = "异常-汇兑往账:" + rmSndExceAmt;
		String cpfsRmSndGJAmtMsg = "归集-汇兑往账:" + rmSndGJAmt;
		String cpfsRmRcvBizDNMsg = "正常-汇兑来账:" + rmRcvBizDN;
		String cpfsRmRcvExceDNMsg = "异常-汇兑来账:" + rmRcvExceDN;
		String cpfsRmRcvGJDNMsg = "归集-汇兑来账:" + rmRcvGJDN;
		String cpfsRmRcvBizAmtMsg = "正常-汇兑来账:" + rmRcvBizAmt;
		String cpfsRmRcvExceAmtMsg = "异常-汇兑来账:" + rmRcvExceAmt;
		String cpfsRmRcvGJAmtMsg = "归集-汇兑来账:" + rmRcvGJAmt;
		String cpfsDebitSndBizDNMsg = "正常-借记往账:" + debitSndBizDN;
		String cpfsDebitSndExceDNMsg = "异常-借记往账:" + debitSndExceDN;
		String cpfsDebitSndBizAmtMsg = "正常-借记往账:" + debitSndBizAmt;
		String cpfsDebitSndExceAmtMsg = "异常-借记往账:" + debitSndExceAmt;
		String cpfsCreditSndBizDNMsg = "正常-贷记往账:" + creditSndBizDN;
		String cpfsCreditSndExceDNMsg = "异常-贷记往账:" + creditSndExceDN;
		String cpfsCreditSndBizAmtMsg = "正常-贷记往账:" + creditSndBizAmt;
		String cpfsCreditSndExceAmtMsg = "异常-贷记往账:" + creditSndExceAmt;
		String cpfsTotDNMsg = "总计:" + cpfsNumTot;
		String cpfsTolAmtMsg = "总计:" + cpfsAmtTot;
		String cpfsLog = String.format("\n|%" + StringUtil.calStrCenterLen(itemLength+2,cpfsHead1,true) + "s%s%" + StringUtil.calStrCenterLen(itemLength+2,cpfsHead1,false) + "s|%" + StringUtil.calStrCenterLen(itemLength+2,cpfsHead2,true) + "s%s%" + StringUtil.calStrCenterLen(itemLength+2,cpfsHead2,false) + "s|\n" +
									   "|%" + StringUtil.calStrCenterLen(itemLength,cpfsRmSndBizDNMsg,true) + "s%s%" + StringUtil.calStrCenterLen(itemLength,cpfsRmSndBizDNMsg,false) + "s|%" + StringUtil.calStrCenterLen(itemLength,cpfsRmSndBizAmtMsg,true) + "s%s%" + StringUtil.calStrCenterLen(itemLength,cpfsRmSndBizAmtMsg,false) + "s|\n" +
									   "|%" + StringUtil.calStrCenterLen(itemLength,cpfsRmSndExceDNMsg,true) + "s%s%" + StringUtil.calStrCenterLen(itemLength,cpfsRmSndExceDNMsg,false) + "s|%" + StringUtil.calStrCenterLen(itemLength,cpfsRmSndExceAmtMsg,true) + "s%s%" + StringUtil.calStrCenterLen(itemLength,cpfsRmSndExceAmtMsg,false) + "s|\n" +
									   "|%" + StringUtil.calStrCenterLen(itemLength,cpfsRmSndGJDNMsg,true) + "s%s%" + StringUtil.calStrCenterLen(itemLength,cpfsRmSndGJDNMsg,false) + "s|%" + StringUtil.calStrCenterLen(itemLength,cpfsRmSndGJAmtMsg,true) + "s%s%" + StringUtil.calStrCenterLen(itemLength,cpfsRmSndGJAmtMsg,false) + "s|\n" +
									   "|%" + StringUtil.calStrCenterLen(itemLength,cpfsRmRcvBizDNMsg,true) + "s%s%" + StringUtil.calStrCenterLen(itemLength,cpfsRmRcvBizDNMsg,false) + "s|%" + StringUtil.calStrCenterLen(itemLength,cpfsRmRcvBizAmtMsg,true) + "s%s%" + StringUtil.calStrCenterLen(itemLength,cpfsRmRcvBizAmtMsg,false) + "s|\n" +
									   "|%" + StringUtil.calStrCenterLen(itemLength,cpfsRmRcvExceDNMsg,true) + "s%s%" + StringUtil.calStrCenterLen(itemLength,cpfsRmRcvExceDNMsg,false) + "s|%" + StringUtil.calStrCenterLen(itemLength,cpfsRmRcvExceAmtMsg,true) + "s%s%" + StringUtil.calStrCenterLen(itemLength,cpfsRmRcvExceAmtMsg,false) + "s|\n" +
									   "|%" + StringUtil.calStrCenterLen(itemLength,cpfsRmRcvGJDNMsg,true) + "s%s%" + StringUtil.calStrCenterLen(itemLength,cpfsRmRcvGJDNMsg,false) + "s|%" + StringUtil.calStrCenterLen(itemLength,cpfsRmRcvGJAmtMsg,true) + "s%s%" + StringUtil.calStrCenterLen(itemLength,cpfsRmRcvGJAmtMsg,false) + "s|\n" +
									   "|%" + StringUtil.calStrCenterLen(itemLength,cpfsDebitSndBizDNMsg,true) + "s%s%" + StringUtil.calStrCenterLen(itemLength,cpfsDebitSndBizDNMsg,false) + "s|%" + StringUtil.calStrCenterLen(itemLength,cpfsDebitSndBizAmtMsg,true) + "s%s%" + StringUtil.calStrCenterLen(itemLength,cpfsDebitSndBizAmtMsg,false) + "s|\n" +
									   "|%" + StringUtil.calStrCenterLen(itemLength,cpfsDebitSndExceDNMsg,true) + "s%s%" + StringUtil.calStrCenterLen(itemLength,cpfsDebitSndExceDNMsg,false) + "s|%" + StringUtil.calStrCenterLen(itemLength,cpfsDebitSndExceAmtMsg,true) + "s%s%" + StringUtil.calStrCenterLen(itemLength,cpfsDebitSndExceAmtMsg,false) + "s|\n" +
									   "|%" + StringUtil.calStrCenterLen(itemLength,cpfsCreditSndBizDNMsg,true) + "s%s%" + StringUtil.calStrCenterLen(itemLength,cpfsCreditSndBizDNMsg,false) + "s|%" + StringUtil.calStrCenterLen(itemLength,cpfsCreditSndBizAmtMsg,true) + "s%s%" + StringUtil.calStrCenterLen(itemLength,cpfsCreditSndBizAmtMsg,false) + "s|\n" +
									   "|%" + StringUtil.calStrCenterLen(itemLength,cpfsCreditSndExceDNMsg,true) + "s%s%" + StringUtil.calStrCenterLen(itemLength,cpfsCreditSndExceDNMsg,false) + "s|%" + StringUtil.calStrCenterLen(itemLength,cpfsCreditSndExceAmtMsg,true) + "s%s%" + StringUtil.calStrCenterLen(itemLength,cpfsCreditSndExceAmtMsg,false) + "s|\n" +
									   "|%" + StringUtil.calStrCenterLen(itemLength+4,cpfsTotDNMsg,true) + "s%s%" + StringUtil.calStrCenterLen(itemLength+4,cpfsTotDNMsg,false) + "s|%" + StringUtil.calStrCenterLen(itemLength+4,cpfsTolAmtMsg,true) + "s%s%" + StringUtil.calStrCenterLen(itemLength+4,cpfsTolAmtMsg,false) + "s|",
									   "",cpfsHead1,"","",cpfsHead2,"",
									   "",cpfsRmSndBizDNMsg,"","",cpfsRmSndBizAmtMsg,"",
									   "",cpfsRmSndExceDNMsg,"","",cpfsRmSndExceAmtMsg,"",
									   "",cpfsRmSndGJDNMsg,"","",cpfsRmSndGJAmtMsg,"",
								       "",cpfsRmRcvBizDNMsg,"","",cpfsRmRcvBizAmtMsg,"",
									   "",cpfsRmRcvExceDNMsg,"","",cpfsRmRcvExceAmtMsg,"",
									   "",cpfsRmRcvGJDNMsg,"","",cpfsRmRcvGJAmtMsg,"",
									   "",cpfsDebitSndBizDNMsg,"","",cpfsDebitSndBizAmtMsg,"",
									   "",cpfsDebitSndExceDNMsg,"","",cpfsDebitSndExceAmtMsg,"",
									   "",cpfsCreditSndBizDNMsg,"","",cpfsCreditSndBizAmtMsg,"",
									   "",cpfsCreditSndExceDNMsg,"","",cpfsCreditSndExceAmtMsg,"",
									   "",cpfsTotDNMsg,"","",cpfsTolAmtMsg,"");

		String ncsHead1 = "NCS笔数";
		String ncsHead2 = "NCS金额";
		String ncsRmSndDNMsg = "汇兑往账:" + ncsRmSndDN;
		String ncsRmSndAmtMsg = "汇兑往账:" + ncsRmSndAmt;
		String ncsRmRcvDNMsg = "汇兑来账:" + ncsRmRcvDN;
		String ncsRmRcvAmtMsg = "汇兑来账:" + ncsRmRcvAmt;
		String ncsDebitSndDNMsg = "借记往账:" + ncsDebitSndDN;
		String ncsDebitSndAmtMsg = "借记往账:" + ncsDebitSndAmt;
		String ncsCreditSndDNMsg = "贷记往账:" + ncsCreditSndDN;
		String ncsCreditSndAmtMsg = "贷记往账:" + ncsCreditSndAmt;
		String ncsTotDNMsg = "总计:" + ncsNumTot;
		String ncsTolAmtMsg = "总计:" + ncsAmtTot;
		String ncsLog = String.format("\n|%" + StringUtil.calStrCenterLen(itemLength+4,ncsHead1,true) + "s%s%" + StringUtil.calStrCenterLen(itemLength+4,ncsHead1,false) + "s|%" + StringUtil.calStrCenterLen(itemLength+4,ncsHead2,true) + "s%s%" + StringUtil.calStrCenterLen(itemLength+4,ncsHead2,false) + "s|\n" +
									  "|%" + StringUtil.calStrCenterLen(itemLength+2,ncsRmSndDNMsg,true) + "s%s%" + StringUtil.calStrCenterLen(itemLength+2,ncsRmSndDNMsg,false) + "s|%" + StringUtil.calStrCenterLen(itemLength+2,ncsRmSndAmtMsg,true) + "s%s%" + StringUtil.calStrCenterLen(itemLength+2,ncsRmSndAmtMsg,false) + "s|\n" +
									  "|%" + StringUtil.calStrCenterLen(itemLength+2,ncsRmRcvDNMsg,true) + "s%s%" + StringUtil.calStrCenterLen(itemLength+2,ncsRmRcvDNMsg,false) + "s|%" + StringUtil.calStrCenterLen(itemLength+2,ncsRmRcvAmtMsg,true) + "s%s%" + StringUtil.calStrCenterLen(itemLength+2,ncsRmRcvAmtMsg,false) + "s|\n" +
									  "|%" + StringUtil.calStrCenterLen(itemLength+2,ncsDebitSndDNMsg,true) + "s%s%" + StringUtil.calStrCenterLen(itemLength+2,ncsDebitSndDNMsg,false) + "s|%" + StringUtil.calStrCenterLen(itemLength+2,ncsDebitSndAmtMsg,true) + "s%s%" + StringUtil.calStrCenterLen(itemLength+2,ncsDebitSndAmtMsg,false) + "s|\n" +
									  "|%" + StringUtil.calStrCenterLen(itemLength+2,ncsCreditSndDNMsg,true) + "s%s%" + StringUtil.calStrCenterLen(itemLength+2,ncsCreditSndDNMsg,false) + "s|%" + StringUtil.calStrCenterLen(itemLength+2,ncsCreditSndAmtMsg,true) + "s%s%" + StringUtil.calStrCenterLen(itemLength+2,ncsCreditSndAmtMsg,false) + "s|\n" +
									  "|%" + StringUtil.calStrCenterLen(itemLength+4,ncsTotDNMsg,true) + "s%s%" + StringUtil.calStrCenterLen(itemLength+4,ncsTotDNMsg,false) + "s|%" + StringUtil.calStrCenterLen(itemLength+4,ncsTolAmtMsg,true) + "s%s%" + StringUtil.calStrCenterLen(itemLength+4,ncsTolAmtMsg,false) + "s|",
									  "",ncsHead1,"","",ncsHead2,"",
									  "",ncsRmSndDNMsg,"","",ncsRmSndAmtMsg,"",
									  "",ncsRmRcvDNMsg,"","",ncsRmRcvAmtMsg,"",
									  "",ncsDebitSndDNMsg,"","",ncsDebitSndAmtMsg,"",
									  "",ncsCreditSndDNMsg,"","",ncsCreditSndAmtMsg,"",
									  "",ncsTotDNMsg,"","",ncsTolAmtMsg,"");

		LOGGER.info(cpfsLog);
		LOGGER.info(ncsLog);

		if(!cpfsNumTot.equals(ncsNumTot)){
			String errMsg = String.format("前置总笔数[%s]与NCS总笔数[%s]不一致，无法生成[%s]日对账文件",cpfsNumTot,ncsNumTot,DateUtil.date3Str(chkAD));
			throw new BaseException(RespCd.NCPS9004,errMsg);
		}

		BigDecimal diffAmtTot = cpfsAmtTot.subtract(ncsAmtTot).abs();
		if(!(diffAmtTot.compareTo(new BigDecimal("0.01")) == -1)){
			String errMsg = String.format("前置总金额[%s]与NCS总金额[%s]不一致，无法生成[%s]日对账文件",cpfsAmtTot,ncsAmtTot,DateUtil.date3Str(chkAD));
			throw new BaseException(RespCd.NCPS9004,errMsg);
		}

		monitorLOGGER.info("[{}]日前置异常笔数，汇兑往账=[{}]，汇兑来账=[{}]，借记往账=[{}]，贷记往账=[{}]",DateUtil.date3Str(chkAD),rmSndExceDN,rmRcvExceDN,debitSndExceDN,creditSndExceDN);

	}

	/**
	 * 
	 * Description:汇总NCS账务数据到汇总表
	 *
	 * @author jjw
	 * @data 2019年9月12日
	 */
	@Override
	protected void sumData() {

		try{
			//根据业务系统，汇总账务表中各报文数据
			delSumData();
			doSumData();
			
			updateChkASchedule("1",null,null,null,null,null);
		}catch(Exception e){
			String errMsg = String.format("业务系统[%s]与[%s]通道[%s]日的对账文件生成失败，汇总账务数据出错", bizSysId,pCId,DateUtil.date3Str(chkAD));
			throw new BaseException(RespCd.NCPS9004,e,errMsg);
		}
		
	}

	/**
	 *
	 * Description:生成对账文件
	 *
	 * @author jjw
	 * @data 2019年9月13日
	 */
	@Override
	protected void genF(){

		doSumF();
		doRmDtl();
		doDrDtl();
		doOtrDtl();

	}
	
	/**
	 * 
	 * Description:上传对账文件tar包
	 *
	 * @author jjw
	 * @data 2019年9月16日
	 */
	@Override
	protected void upldF() {
		
		String tarFilePath = "";
		try{
			//获取上传配置
			Map<String,ToParm> config = DbToParmToCache.getToParm();
			String localPath = config.get("ncs_chka/localPath").getDataVal();
			String mfeConfig = config.get("ncs_chka/bizF_upload_infos").getDataVal();
			String[] mfeParams = mfeConfig.split(",");
			String host = mfeParams[0];
			Integer port = Integer.parseInt(mfeParams[1]);
			String username = mfeParams[2];
			String password = Encry.decryptString(mfeParams[3]);
			Integer timeout = Integer.parseInt(mfeParams[4]);
			String remotePath = mfeParams[5] + this.bizSysId + mfeParams[6];
			
			//生成tar包
			String chkAFDirPath = localPath + File.separator + bizSysId;
			tarFilePath = localPath + File.separator + DateUtil.date4Str(chkAD) + bizSysId + ".tar";
			FileUtil.tarFile(new String[]{chkAFDirPath},"UTF-8", tarFilePath,"GBK");
			
			//上传tar包
			LOGGER.info("上传对账文件[{}]，MFE配置信息：\nIP=[{}]\n端口=[{}]\n用户=[{}]\n超时时间=[{}]ms\n远程文件路径=[{}]\n本地文件保存路径=[{}]",
					tarFilePath,host,port,username,timeout,remotePath,tarFilePath);
			SFTPTransfer sftp = new SFTPTransfer();
			Map<String,String> upldResult = sftp.uploadFile(host, port, username, "", "", password, timeout/1000, tarFilePath, remotePath);
			if("0".equals(upldResult.get("retCode"))){
				throw new BaseException(RespCd.NCPS9400,upldResult.get("retInfo"));
			}
			
			updateChkASchedule("2",null,null,remotePath,tarFilePath.substring(tarFilePath.lastIndexOf(File.separator) + 1),null);
		}catch(BaseException e){
			String errMsg = String.format("业务系统[%s]与[%s]通道[%s]日的对账文件生成失败，压缩上传对账文件[%s]失败，原因：[%s]", bizSysId,pCId,DateUtil.date3Str(chkAD),tarFilePath,e.getErrorDesc());
			throw new BaseException(RespCd.NCPS9004,e,errMsg);
		}catch(Exception e){
			String errMsg = String.format("业务系统[%s]与[%s]通道[%s]日的对账文件生成失败，压缩上传对账文件[%s]失败，原因：[%s]", bizSysId,pCId,DateUtil.date3Str(chkAD),tarFilePath,e.getMessage());
			throw new BaseException(RespCd.NCPS9004,e,errMsg);
		}
		
	}
	
	/**
	 * 
	 * Description:向业务系统发送下载文件通知
	 *
	 * @author jjw
	 * @data 2019年9月16日
	 */
	@Override
	protected void ntcBizSys() {
		
		try{
			Map<String,ToParm> config = DbToParmToCache.getToParm();
			String localPath = config.get("ncs_chka/localPath").getDataVal();

			//删除临时对账文件
			String chkAFDirPath = localPath + File.separator + bizSysId;
			String tarFilePath = localPath + File.separator + DateUtil.date4Str(chkAD) + bizSysId + ".tar";
			FileUtil.delFile(new File(chkAFDirPath));
			FileUtil.delFile(new File(tarFilePath));
			
			if("NGPS|NVAS".contains(bizSysId)){
				sndNPS919();
			}else{
				String errMsg = String.format("新业务系统[%s]还未确定如何下发对账文件通知",bizSysId);
				throw new Exception(errMsg);
			}
			
			updateChkASchedule("3","1",new Date(),null,null,"无");
		}catch(BaseException e){
			String errMsg = String.format("业务系统[%s]与[%s]通道[%s]日的对账文件生成完成，但向该业务系统发送通知失败，原因：[%s]", bizSysId,pCId,DateUtil.date3Str(chkAD),e.getErrorDesc());
			throw new BaseException(RespCd.NCPS9004,e,errMsg);
		}catch(Exception e){
			String errMsg = String.format("业务系统[%s]与[%s]通道[%s]日的对账文件生成完成，但向该业务系统发送通知失败，原因：[%s]", bizSysId,pCId,DateUtil.date3Str(chkAD),e.getMessage());
			throw new BaseException(RespCd.NCPS9004,e,errMsg);
		}
		
	}

	/**
	 *
	 * Description:删除当前业务系统该清算日期汇总数据
	 *
	 * @author jjw
	 * @data 2019年9月13日
	 */
	private void delSumData(){

		String delSql = "delete from td_biz_chka_sum where chkAD = ? and bizSysId = ?";

		JdbcTemplate jdbcTemplate = (JdbcTemplate) SpringUtil.getBean("jdbcTemplate");

		jdbcTemplate.update(delSql,chkAD,bizSysId);

	}

	/**
	 *
	 * Description:汇总数据
	 *
	 * @author jjw
	 * @data 2019年9月13日
	 */
	private void doSumData(){

		//汇总借记
		String insertDrSumSql = "insert into td_biz_chka_sum(bizSysId,chkAD,bNo,msgNo,bizTyp,contFlg,curKnd,drTotDN,drTotAmt,loanTotDN,loanTotAmt,crLoanTotDN,crLoanTotAmt,rscsCrLoanTotDN,rscsCrLoanTotAmt) values";

		//汇总贷记
		String insertLoadSumSql = "insert into td_biz_chka_sum(bizSysId,chkAD,bNo,msgNo,bizTyp,contFlg,curKnd,drTotDN,drTotAmt,loanTotDN,loanTotAmt,crLoanTotDN,crLoanTotAmt,rscsCrLoanTotDN,rscsCrLoanTotAmt) values";

		//汇兑往账
		String rmSndSql = "select sndSysId,cpfsD,acqOrgId,chlXCd,bizTyp,xDirn,curTyp,count(id) drTotDN,sum(xAmt) drTotAmt " +
						  "from tb_a " +
						  "where chlXCd = '" + MsgCdOrTpEnum.MSGCD_120.getCode() + "' and xDirn = '12' and pCId = ? and cpfsD = ? and chkASta = '1' and acqOrgId = acqOrgId and sndSysId = ? " +
						  "group by sndSysId,cpfsD,acqOrgId,chlXCd,bizTyp,xDirn,curTyp";

		//汇兑来账
		String rmRcvSql = "select rcvSysId bizSysId,cpfsD,acqOrgId,chlXCd,bizTyp,xDirn,curTyp,count(id) loanTotDN,sum(xAmt) loanTotAmt " +
						  "from tb_a " +
						  "where chlXCd = '" + MsgCdOrTpEnum.MSGCD_120.getCode() + "' and xDirn = '21' and pCId = ? and cpfsD = ? and chkASta = '1' and acqOrgId = acqOrgId and rcvSysId = ? " +
						  "group by rcvSysId,cpfsD,acqOrgId,chlXCd,bizTyp,xDirn,curTyp";

		//借记往账
		String debitSndSql = "select sndSysId bizSysId,cpfsD,acqOrgId,chlXCd,bizTyp,xDirn,curTyp,count(id) loanTotDN,sum(xAmt) loanTotAmt " +
							 "from tb_a " +
							 "where chlXCd = '" + MsgCdOrTpEnum.MSGCD_142.getCode() + "' and xDirn = '12' and pCId = ? and cpfsD = ? and chkASta = '1' and acqOrgId = acqOrgId and sndSysId = ? " +
							 "group by sndSysId,cpfsD,acqOrgId,chlXCd,bizTyp,xDirn,curTyp";

		//贷记往账
		String creditSndSql = "select sndSysId,cpfsD,acqOrgId,chlXCd,bizTyp,xDirn,curTyp,count(id) drTotDN,sum(xAmt) drTotAmt " +
							  "from tb_a " +
							  "where chlXCd = '" + MsgCdOrTpEnum.MSGCD_140.getCode() + "' and xDirn = '12' and pCId = ? and cpfsD = ? and chkASta = '1' and acqOrgId = acqOrgId and sndSysId = ? " +
							  "group by sndSysId,cpfsD,acqOrgId,chlXCd,bizTyp,xDirn,curTyp";

		JdbcTemplate jdbcTemplate = (JdbcTemplate) SpringUtil.getBean("jdbcTemplate");

		List<Map<String,Object>> drSumList = new ArrayList<Map<String,Object>>();
		drSumList.addAll(jdbcTemplate.queryForList(rmSndSql,pCId,chkAD,bizSysId));
		drSumList.addAll(jdbcTemplate.queryForList(creditSndSql,pCId,chkAD,bizSysId));

		List<Map<String,Object>> loadSumList = new ArrayList<Map<String,Object>>();
		loadSumList.addAll(jdbcTemplate.queryForList(rmRcvSql,pCId,chkAD,bizSysId));
		loadSumList.addAll(jdbcTemplate.queryForList(debitSndSql,pCId,chkAD,bizSysId));

		if(drSumList.size() > 0){
			for(Map<String,Object> drSum : drSumList){
				insertDrSumSql += "('" + drSum.get("sndSysId") + "','" + drSum.get("cpfsD") + "','" + drSum.get("acqOrgId") +
								  "','" + drSum.get("chlXCd") + "','" + drSum.get("bizTyp") + "','" + drSum.get("xDirn") +
								  "','" + drSum.get("curTyp") + "'," + drSum.get("drTotDN") + "," + drSum.get("drTotAmt") +
								  ",0,0.00,0,0.00,0,0.00),";
			}

			insertDrSumSql = insertDrSumSql.substring(0,insertDrSumSql.length() - 1);

			jdbcTemplate.update(insertDrSumSql);
		}

		if(loadSumList.size() > 0){
			for (Map<String,Object> loadSum : loadSumList){
				insertLoadSumSql += "('" + loadSum.get("bizSysId") + "','" + loadSum.get("cpfsD") + "','" + loadSum.get("acqOrgId") +
									"','" + loadSum.get("chlXCd") + "','" + loadSum.get("bizTyp") + "','" + loadSum.get("xDirn") +
									"','" + loadSum.get("curTyp") + "',0,0.00," + loadSum.get("loanTotDN") + "," + loadSum.get("loanTotAmt") +
									",0,0.00,0,0.00),";
			}

			insertLoadSumSql = insertLoadSumSql.substring(0,insertLoadSumSql.length() - 1);

			jdbcTemplate.update(insertLoadSumSql);
		}

	}

	/**
	 *
	 * Description:生成汇总文件
	 *
	 * @author jjw
	 * @data 2019年9月13日
	 */
	private void doSumF() {

		String sumFNm = "";
		try{
			TbBizChkaSumMapper tbBizChkaSumMapper = sqlSession.getMapper(TbBizChkaSumMapper.class);
			List<TbBizChkaSum> tbBizChkaSums = tbBizChkaSumMapper.queryNCSSumByBizSysId(bizSysId, chkAD);
			List<String> sumFDatas = genSumFData(tbBizChkaSums);

			Map<String,ToParm> config = DbToParmToCache.getToParm();
			String localPath = config.get("ncs_chka/localPath").getDataVal();
			sumFNm = localPath + File.separator + bizSysId + File.separator + NCSChkAFileTypEnum.SUM_CHKA.getCode() + "CNY" + bizSysId + DateUtil.date4Str(chkAD);
			FileUtil.writeFile(sumFNm, sumFDatas);
			LOGGER.info("已生成业务系统[{}]的[{}]通道[{}]日的临时汇总对账文件：[{}]",bizSysId,pCId,DateUtil.date3Str(chkAD),sumFNm);
		}catch(Exception e){
			String errMsg = String.format("业务系统[%s]与[%s]通道[%s]日的对账文件生成失败，汇总文件[%s]生成失败", bizSysId,pCId,DateUtil.date3Str(chkAD),sumFNm);
			throw new BaseException(RespCd.NCPS9004,e,errMsg);
		}

	}
	
	/**
	 * 
	 * Description:处理汇兑明细
	 *
	 * @author jjw
	 * @data 2019年9月16日
	 */
	private void doRmDtl(){
		
		String rmFNm = "";
		
		try{
			Map<String,ToParm> config = DbToParmToCache.getToParm();
			String localPath = config.get("ncs_chka/localPath").getDataVal();
			rmFNm = localPath + File.separator + bizSysId + File.separator + NCSChkAFileTypEnum.RM_BIZ.getCode() + "CNY" + bizSysId + DateUtil.date4Str(chkAD);
			
			TbAMapper tbAMapper = sqlSession.getMapper(TbAMapper.class);
			List<TbA> tbAs = tbAMapper.queryNCSRmDtlByBizSysId(bizSysId, chkAD);
			List<String> rmFDatas = genRmFData(tbAs);
			
			FileUtil.writeFile(rmFNm, rmFDatas);
			
			LOGGER.info("已生成业务系统[{}]的[{}]通道[{}]日的临时汇兑明细对账文件：[{}]",bizSysId,pCId,DateUtil.date3Str(chkAD),rmFNm);
		}catch(Exception e){
			String errMsg = String.format("业务系统[%s]与[%s]通道[%s]日的对账文件生成失败，汇兑明细文件[%s]生成失败", bizSysId,pCId,DateUtil.date3Str(chkAD),rmFNm);
			throw new BaseException(RespCd.NCPS9004,e,errMsg);
		}
		
	}
	
	/**
	 * 
	 * Description:处理借贷记明细
	 *
	 * @author jjw
	 * @data 2019年9月16日
	 */
	private void doDrDtl(){
		
		String drFNm = "";
		
		try{
			Map<String,ToParm> config = DbToParmToCache.getToParm();
			String localPath = config.get("ncs_chka/localPath").getDataVal();
			drFNm = localPath + File.separator + bizSysId + File.separator + NCSChkAFileTypEnum.NOW_DRCR_BIZ.getCode() + "CNY" + bizSysId + DateUtil.date4Str(chkAD);
			
			TbAMapper tbAMapper = sqlSession.getMapper(TbAMapper.class);
			List<TbA> tbAs = tbAMapper.queryNCSDrDtlByBizSysId(bizSysId, chkAD);
			List<String> drFDatas = genDrFData(tbAs);
			
			FileUtil.writeFile(drFNm, drFDatas);
			
			LOGGER.info("已生成业务系统[{}]的[{}]通道[{}]日的临时借贷记明细对账文件：[{}]",bizSysId,pCId,DateUtil.date3Str(chkAD),drFNm);
		}catch(Exception e){
			String errMsg = String.format("业务系统[%s]与[%s]通道[%s]日的对账文件生成失败，借贷记明细文件[%s]生成失败", bizSysId,pCId,DateUtil.date3Str(chkAD),drFNm);
			throw new BaseException(RespCd.NCPS9004,e,errMsg);
		}
		
	}
	
	/**
	 * 
	 * Description:处理其他空明细
	 *
	 * @author jjw
	 * @data 2019年9月16日
	 */
	private void doOtrDtl(){
		
		try{
			String excludeF = NCSChkAFileTypEnum.SUM_CHKA.getCode() + "|" + NCSChkAFileTypEnum.RM_BIZ.getCode() + "|" + NCSChkAFileTypEnum.NOW_DRCR_BIZ.getCode();
			String noCurTypF = NCSChkAFileTypEnum.QRY_CHK.getCode() + "|" + NCSChkAFileTypEnum.PROT_MNG_BIZ.getCode();
			
			Map<String,ToParm> config = DbToParmToCache.getToParm();
			String localPath = config.get("ncs_chka/localPath").getDataVal();
			
			NCSChkAFileTypEnum[] enums = NCSChkAFileTypEnum.values();
			for(NCSChkAFileTypEnum nCSChkAFileTyp : enums){
				if(excludeF.contains(nCSChkAFileTyp.getCode())){
					continue;
				}
				String fNm;
				if(noCurTypF.contains(nCSChkAFileTyp.getCode())){
					fNm = localPath + File.separator + bizSysId + File.separator + nCSChkAFileTyp.getCode() + bizSysId + DateUtil.date4Str(chkAD);
				}else{
					fNm = localPath + File.separator + bizSysId + File.separator + nCSChkAFileTyp.getCode() + "CNY" + bizSysId + DateUtil.date4Str(chkAD);
				}
				FileUtil.writeFile(fNm, new ArrayList<String>());
			}
		}catch(Exception e){
			String errMsg = String.format("业务系统[%s]与[%s]通道[%s]日的对账文件生成失败，其他空明细文件生成失败", bizSysId,pCId,DateUtil.date3Str(chkAD));
			throw new BaseException(RespCd.NCPS9004,e,errMsg);
		}
		
	}
	
	/**
	 * 
	 * Description:生成汇总文件中的数据
	 *
	 * @author jjw
	 * @data 2019年9月13日
	 */
	private List<String> genSumFData(List<TbBizChkaSum> tbBizChkaSums){
		
		List<String> sumFDatas = new ArrayList<String>();
		if(tbBizChkaSums != null && tbBizChkaSums.size() > 0){
			for(TbBizChkaSum tbBizChkaSum : tbBizChkaSums){
				StringBuilder sb = new StringBuilder();
				sb.append(DateUtil.date4Str(tbBizChkaSum.getChkAD())).append("|")
				.append(tbBizChkaSum.getBizSysId()).append("|")
				.append(tbBizChkaSum.getMsgNo()).append("|")
				.append(tbBizChkaSum.getBizTyp()).append("|")
				.append("12".equals(tbBizChkaSum.getContFlg()) ? "0" : "1").append("|")
				.append(tbBizChkaSum.getCurKnd()).append("|")
				.append(tbBizChkaSum.getDrTotDN()).append("|")
				.append(tbBizChkaSum.getDrTotAmt()).append("|")
				.append(tbBizChkaSum.getLoanTotDN()).append("|")
				.append(tbBizChkaSum.getLoanTotAmt()).append("|")
				.append(tbBizChkaSum.getCrLoanTotDN()).append("|")
				.append(tbBizChkaSum.getCrLoanTotAmt()).append("|")
				.append(tbBizChkaSum.getRscsCrLoanTotDN()).append("|")
				.append(tbBizChkaSum.getRscsCrLoanTotAmt());
				sumFDatas.add(sb.toString());
			}
		}
		
		return sumFDatas;
		
	}
	
	/**
	 * 
	 * Description:生成汇兑明细文件中的数据
	 *
	 * @author jjw
	 * @data 2019年9月16日
	 */
	private List<String> genRmFData(List<TbA> tbAs){
		
		List<String> rmFDatas = new ArrayList<String>();
		if(tbAs != null && tbAs.size() > 0){
			for(TbA tbA : tbAs){
				StringBuilder sb = new StringBuilder();
				sb.append(tbA.getChlXCd()).append("|")
				.append(tbA.getBizTyp()).append("|")
				.append(tbA.getBizCls()).append("|")
				.append(tbA.getChlSeqNo()).append("|")
				.append(tbA.getPyrBrchNo()).append("|")
				.append(tbA.getPyrBNo()).append("|")
				.append(tbA.getChlSeqNo()).append("|")
				.append(tbA.getEndToEndId()).append("|")
				.append(DateUtil.getISODateTime(tbA.getCreTm())).append("|")
				.append("07").append("|")//交易渠道类型填什么，目前示例对账文件中填的是07
				.append("NPS").append("|")//系统编号填什么，目前示例对账文件中填的是NPS
				.append(tbA.getPyrNm()).append("|")
				.append("").append("|")//付款人地址填什么，目前示例对账文件中没填
				.append(tbA.getPyrBrchNo()).append("|")
				.append(tbA.getPyrA()).append("|")
				.append(tbA.getPyrBrchNo()).append("|")
				.append(tbA.getPyeNm()).append("|")
				.append("").append("|")//收款人地址填什么，目前示例对账文件中没填
				.append(tbA.getPyeBrchNo()).append("|")
				.append(tbA.getPyeA()).append("|")
				.append(tbA.getPyeBrchNo()).append("|")
				.append(tbA.getCurTyp()).append("|")
				.append(tbA.getxAmt()).append("|")
				.append(tbA.getFeAmt()).append("|")
				.append(StringUtils.isNotBlank(tbA.getReserved1()) ? tbA.getReserved1() : "").append("|")//备注，往账的也填reserved1吗
				.append("").append("|")//用途，目前示例对账文件中没填
				.append("").append("|")//原发起行行号，目前示例对账文件中没填
				.append("").append("|")//原托金额，目前示例对账文件中没填
				.append(StringUtils.isNotBlank(tbA.getReserved2()) ? tbA.getReserved2() : "").append("|")//原报文标识号,目前120来账退汇报文中的原报文标识号存在该字段中
				.append("").append("|")//原报文编号，目前示例对账文件中没填
				.append("").append("|")//退汇原因，目前示例对账文件中没填
				.append("").append("|")//多付金额，目前示例对账文件中没填
				.append("").append("|")//所属期间，目前示例对账文件中没填
				.append("").append("|")//拒付金额，目前示例对账文件中没填
				.append("").append("|")//支付金额，目前示例对账文件中没填
				.append("").append("|")//收费单位流水号，目前示例对账文件中没填
				.append("").append("|")//收费附言，目前示例对账文件中没填
				.append("").append("|")//票据号码，目前示例对账文件中没填
				.append("").append("|")//票据日期，目前示例对账文件中没填
				.append("").append("|")//票据种类，目前示例对账文件中没填
				.append("").append("|")//缴费类型，目前示例对账文件中没填
				.append("").append("|")//赔偿金额，目前示例对账文件中没填
				.append(DateUtil.date4Str(tbA.getCpfsD())).append("|")
				.append("NPSI0000").append("|")//响应码填什么，目前示例对账文件中填的是NPSI0000
				.append("").append("|")//业务拒绝信息填什么，目前示例对账文件中没填
				.append(StringUtils.isNotBlank(tbA.getRespSta()) ? tbA.getRespSta() : "").append("|")
				.append(tbA.getPyeBNo()).append("|")
				.append(tbA.getPyeBrchNo()).append("|")
				.append("12".equals(tbA.getxDirn()) ? "0" : "1");
				rmFDatas.add(sb.toString());
			}
		}
		
		return rmFDatas;
		
	}

	/**
	 *
	 * Description:生成借贷记明细文件中的数据
	 *
	 * @author jjw
	 * @data 2019年9月16日
	 */
	private List<String> genDrFData(List<TbA> tbAs){
		
		List<String> drFDatas = new ArrayList<String>();
		if(tbAs != null && tbAs.size() > 0){
			for(TbA tbA : tbAs){
				StringBuilder sb = new StringBuilder();
				sb.append(tbA.getChlXCd()).append("|")
				.append(tbA.getBizTyp()).append("|")
				.append(tbA.getBizCls()).append("|")
				.append(tbA.getChlSeqNo()).append("|")
				.append(tbA.getSndOrgId()).append("|")
				.append("").append("|")//原报文标识号，目前示例对账文件中没填
				.append(DateUtil.getISODateTime(tbA.getCreTm())).append("|")
				.append("1").append("|")//明细业务总笔数，目前示例对账文件中填的是1
				.append(tbA.getChlSeqNo()).append("|")
				.append(tbA.getEndToEndId()).append("|")
				.append("").append("|")//报文头备注，目前示例对账文件中没填
				.append("08").append("|")//交易渠道类型填什么，目前示例对账文件中填的是08
				.append(tbA.getPyrNm()).append("|")
				.append("").append("|")//付款人地址填什么，目前示例对账文件中没填
				.append(tbA.getPyrBrchNo()).append("|")
				.append(tbA.getPyrA()).append("|")
				.append(StringUtils.isNotBlank(tbA.getReserved3()) ? tbA.getReserved3() : "").append("|")//付款人账户类型填什么，目前示例对账文件中有PT03、PT05
				.append(tbA.getPyrBNo()).append("|")
				.append(tbA.getPyrBrchNo()).append("|")
				.append(tbA.getPyeNm()).append("|")
				.append("").append("|")//收款人地址填什么，目前示例对账文件中没填
				.append(tbA.getPyeBrchNo()).append("|")
				.append(tbA.getPyeA()).append("|")
				.append("").append("|")//收款人账户类型填什么，目前示例对账文件中没填
				.append(tbA.getPyeBNo()).append("|")
				.append(tbA.getPyeBrchNo()).append("|")
				.append(tbA.getCurTyp()).append("|")
				.append(tbA.getxAmt()).append("|")
				.append(StringUtils.isNotBlank(tbA.getReserved1()) ? tbA.getReserved1() : "").append("|")//备注
				.append(StringUtils.isNotBlank(tbA.getAddInfo()) ? tbA.getAddInfo() : "").append("|")//附言
				.append("").append("|")//存折余额，目前示例对账文件中没填
				.append("").append("|")//存折号码，目前示例对账文件中没填
				.append("AC05").append("|")//认证方式，目前示例对账文件中填的是AC05
				.append("").append("|")//证件号码，目前示例对账文件中没填
				.append("").append("|")//证件类型，目前示例对账文件中没填
				.append(tbA.getFeAmt()).append("|")//代理行手续费
				.append("PR05").append("|")//业务回执状态，目前示例对账文件中填的是PR05
				.append("").append("|")//业务拒绝原因，目前示例对账文件中没填
				.append("").append("|")//业务拒绝码，目前示例对账文件中没填
				.append("NPSI0000").append("|")//响应码填什么，目前示例对账文件中填的是NPSI0000
				.append(DateUtil.date4Str(tbA.getCpfsD())).append("|")
				.append(StringUtils.isNotBlank(tbA.getRespSta()) ? tbA.getRespSta() : "").append("|")
				.append("").append("|")//原报文编号，目前示例对账文件中没填
				.append("").append("|")//原发起行行号，目前示例对账文件中没填
				.append("").append("|")//IC卡条件码，目前示例对账文件中没填
				.append("").append("|")//终端读取能力，目前示例对账文件中没填
				.append("").append("|")//IC卡验证可靠性标志，目前示例对账文件中没填
				.append("").append("|")//PAN输入方式，目前示例对账文件中没填
				.append("").append("|")//卡序列号，目前示例对账文件中没填
				.append("12".equals(tbA.getxDirn()) ? "0" : "1");
				drFDatas.add(sb.toString());
			}
		}
		
		return drFDatas;
		
	}

	/**
	 *
	 * Description:发送NPS919报文
	 *
	 * @author jjw
	 * @data 2019年9月16日
	 */
	private void sndNPS919() throws Exception {

		Map<String,ToParm> config = DbToParmToCache.getToParm();

		//组装NPS919报文
		String bizMsg = "";
		String bNo = "";
		ToParm toParm = config.get("bizsys_bno_mapper/" + bizSysId);
		if(toParm != null){
			bNo = toParm.getDataVal();
			if(StringUtils.isNotBlank(bNo)){
				bizMsg = ResponseUtil.buildNPS919Xml(bNo,DateUtil.date4Str(chkAD) + bizSysId + ".tar",starringModelNm);
			}else{
				throw new Exception("业务系统[" + bizSysId + "]未配置相应行号");
			}
		}else{
			throw new Exception("业务系统[" + bizSysId + "]未配置相应行号");
		}

		//向业务系统发送通知
		String tpId = DtaInfo.getInstance().getTpId();
		EPOper.put(tpId, "ReqBean[0].head[0].servEcd", RpcOutputInfoEnum.OutPutSvr_CpfsFwdThrdPrtS.getCode());
		EPOper.put(tpId, "ReqBean[0].head[0].sndAppCd", SysEcodeEnum.CPFS.getCode());
		EPOper.put(tpId, "ReqBean[0].head[0].rcvAppCd",bizSysId);
		EPOper.put(tpId, "ReqBean[0].head[0].reqServCd", MsgCdOrTpEnum.MSGCD_919.getCode());
		EPOper.put(tpId, "ReqBean[0].head[0].initrSeqno",System.currentTimeMillis() + "");
		EPOper.getCompSDO(tpId, "ReqBody").setValue("BizMsg",bizMsg);
		String rspsCd = RpcCommUtil.getInstance().rpcFeToGW(tpId, RpcOutputInfoEnum.OutputName_Pub_Cpfsgw.getCode(),RpcOutputInfoEnum.OutPutSvr_CpfsFwdThrdPrtS.getCode());
		if(!rspsCd.equals(RespCd.NCPI0000)){
			throw new Exception((String)EPOper.get(tpId, "RspBean[0].head[0].respsInfoDesc"));
		}

	}

}
